﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;

namespace caculate
{
    public partial class Power_Change : Form
    {
        public Power_Change()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conString = "DSN=caculate;UID=sa;PWD=sa123"; //ODBC的数据源名
            OdbcConnection con1 = new OdbcConnection(conString);
            con1.Open();

            if(label2.Text =="当前状态:可操作错题库")
            {
                if (MessageBox.Show("确定收回权限?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
== DialogResult.OK)
                {
                string sql = "update ChildrenMessage set power='0' where ChildrenName='"+Pow.chdname+"'";
                OdbcCommand com1 = new OdbcCommand(sql, con1);
                int i = Convert.ToInt32(com1.ExecuteNonQuery());
                label2.Text = "当前状态:不可操作";

                this.change.Text = "授予权限";
                }
            }
            else if(label2.Text== "当前状态:不可操作")
            {
                if (MessageBox.Show("确定赋予权限?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
== DialogResult.OK)
                {
                string sql = "update ChildrenMessage set power='1' where ChildrenName='"+Pow.chdname+"' ";
                OdbcCommand com1 = new OdbcCommand(sql, con1);
                int i = Convert.ToInt32(com1.ExecuteNonQuery());
                    label2.Text = "当前状态:可操作错题库";

                this.change.Text = "收回权限";
                }

            }

        }

        public class Pow
        {
            public static int power;
            public static string chdname;
        }

        private void Power_Change_Load(object sender, EventArgs e)
        {
            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            OdbcConnection con1 = new OdbcConnection(conString);
            con1.Open();

            string sql1 = "select Children from ParentMessage where ParentAccount='" + Form1.PublicValue.prtaccount + "'";
            OdbcCommand com1 = new OdbcCommand(sql1, con1);
            Pow.chdname = Convert.ToString(com1.ExecuteScalar());

            label1.Text ="您的孩子" + Pow.chdname;

            OdbcConnection con2 = new OdbcConnection(conString);
            con2.Open();

            string sql2 = "select Power from ChildrenMessage where ChildrenName='" + Pow.chdname + "'";
            OdbcCommand com2 = new OdbcCommand(sql2, con2);
            Pow.power = Convert.ToInt32(com2.ExecuteScalar());
            if(Pow.power==1)
            {
                label2.Text = "当前状态:可操作错题库";
                this.change.Text = "收回权限";
            }
            else
            {
                label2.Text = "当前状态:不可操作";
                this.change.Text = "赋予权限";
            }

        }
    }
}
